﻿#region System Namespaces
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;

    #endregion

#region Styx Namespaces

using Styx;
using Styx.CommonBot.AreaManagement;
using Styx.CommonBot.Profiles;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.Common;
using Styx.WoWInternals.WoWObjects;

#endregion

namespace TeleportDetector
{
    public class TeleportDetector : HBPlugin
    {
        public override string Name
        {
            get { return "TeleportDetector"; }
        }

        public override string Author
        {
            get { return "Giwin"; }
        }

        public override Version Version
        {
            get { return new Version(1, 4); }
        }

        public override bool WantButton
        {
            get { return true; }
        }

        public override void OnButtonPress()
        {
            new Form1().ShowDialog();
        }

        public override void Initialize()
        {
            Logging.Write(Name + " - Loaded Version " + Version);
        }

        public override void Pulse()
        {
            if (StyxWoW.IsInGame && StyxWoW.Me.Name == MIRsettings.Instance.CharacterName && !StyxWoW.Me.IsInInstance)
            {
                Logging.Write(HotspotManager.CurrentHotSpot.Distance2D(HotspotManager.LastHotSpot).ToString(CultureInfo.InvariantCulture));
            }
        }

        public void ToHotspot(string hotspot)
        {
            // <497.833, -399.6852, 263.4409>
            //  

            const string xHotspotRegex = "<[0-9]*.[0-9]*,"; // Remove Nothing
            const string yHotspotRegex = "<[0-9]*.[0-9]*, [0-9]*.[0-9]*.[0-9]*.[0-9]*"; // Remove X
            const string zHotspotRegex = "<[0-9]*.[0-9]*, [0-9]*.[0-9]*.[0-9]*.[0-9]*, [0-9]*.[0-9]*>"; // Remove X and Y

            var xHotspot = Regex.Match(hotspot, xHotspotRegex, RegexOptions.IgnoreCase);
            var yHotspot = Regex.Match(hotspot, yHotspotRegex, RegexOptions.IgnoreCase);
            var zHotspot = Regex.Match(hotspot, zHotspotRegex, RegexOptions.IgnoreCase);

            Logging.Write("X:" + xHotspot);
            Logging.Write("Y:" + yHotspot);
            Logging.Write("Z:" + zHotspot);

            
        }

        public bool VendorDetour()
        {
            try
            {
            return ObjectManager.GetObjectsOfType<WoWUnit>(false, false).Any(unit => unit != null &&
                                                                                     unit.IsValid &&
                                                                                     new VendorManager().GetClosestVendor(Vendor.VendorType.Repair).Entry == unit.Entry &&
                                                                                     new VendorManager().GetClosestVendor(Vendor.VendorType.Repair).Entry2 == unit.Entry &&
                                                                                     new VendorManager().GetClosestVendor(Vendor.VendorType.Food).Entry == unit.Entry &&
                                                                                     new VendorManager().GetClosestVendor(Vendor.VendorType.Food).Entry2 == unit.Entry &&
                                                                                     new VendorManager().GetClosestVendor(Vendor.VendorType.Sell).Entry == unit.Entry &&
                                                                                     new VendorManager().GetClosestVendor(Vendor.VendorType.Sell).Entry2 == unit.Entry);


            }
            catch (Exception e)
            {
                Logging.Write("Vendor Detour: " + e.Message + " " + e.Data + " " + e.Source);
            }
            return false;
        }

        public void Teleported()
        {
            Thread.Sleep(1000);
            Logging.Write("................................");
            Logging.Write("--------- !Teleported! ---------");
            Logging.Write("................................");
            Logging.Write("Time of Incident: " + DateTime.Now);
            Logging.Write("Name of Character: " + StyxWoW.Me.Name);
            Logging.Write("Level of Character: " + StyxWoW.Me.Level);
            Logging.Write("Current Location: " + StyxWoW.Me.Location);
            Logging.Write("Previous Location: " + Bots.Gatherbuddy.Profile.CurrentProfile.Current);
            Logging.Write("Distance between Current and Previous Location: " +
                          StyxWoW.Me.Location.Distance(Bots.Gatherbuddy.Profile.CurrentProfile.Current));
            Logging.Write("Relative Location: " + StyxWoW.Me.RelativeLocation + " | World Location: " +
                          StyxWoW.Me.WorldLocation);
            Logging.Write("Minimap Zone Text: " + StyxWoW.Me.MinimapZoneText);
            Logging.Write("Current Map ID: " + StyxWoW.Me.MapId + " | Current Map Name: " + StyxWoW.Me.MapName);
            Logging.Write("Current Zone ID: " + StyxWoW.Me.ZoneId + " | Current Zone Name: " + StyxWoW.Me.ZoneText);
            Logging.Write("Is Dazed?: " + StyxWoW.Me.Dazed +
                          " | Is Dead?: " + StyxWoW.Me.IsDead +
                          " | Is Fleeing?: " + StyxWoW.Me.Fleeing +
                          " | Is Falling?: " + StyxWoW.Me.IsFalling +
                          " | Is Flying?: " + StyxWoW.Me.IsFlying +
                          " | Is Inndoors?: " + StyxWoW.Me.IsIndoors +
                          " | Is Outdoors?: " + StyxWoW.Me.IsOutdoors +
                          " | Is Inside Instance?: " + StyxWoW.Me.IsInInstance +
                          " | Is OnTransport?: " + StyxWoW.Me.IsOnTransport +
                          " | Is Stunned?: " + StyxWoW.Me.Stunned +
                          " | Is Mounted?: " + StyxWoW.Me.Mounted);
            Logging.Write("-----------------------------------");
            Logging.Write("-----------------------------------");
            Logging.Write("| ***** Message Sent to Say: " + MIRsettings.Instance.Message + " | *****");
            Logging.Write("-----------------------------------");
            Logging.Write("-----------------------------------");

            string dayShort = String.Format("{0:dd}", DateTime.Now);
            string day = String.Format("{0:dddd}", DateTime.Now);
            string month = String.Format("{0:MMMM}", DateTime.Now);
            string year = String.Format("{0:yyyy}", DateTime.Now);

            var newline = Environment.NewLine;
            const string doubleDash = "\\";
            const string nameOfPlugin = "TeleportDetector";
            const string plugins = "Plugins";
            const string mainPath = plugins + doubleDash + nameOfPlugin + doubleDash + "logs" + doubleDash + "";
            string path = mainPath + year + doubleDash + month + doubleDash + dayShort + doubleDash + day + ".txt";
            string pathDirectory = mainPath + year + doubleDash + month + doubleDash + dayShort;
            string timeSent = string.Format("Date: {0}/{1}/{2} - Time: {3}",
                                            DateTime.Now.Day.ToString(CultureInfo.InvariantCulture),
                                            DateTime.Now.Month.ToString(CultureInfo.InvariantCulture),
                                            DateTime.Now.Year.ToString(CultureInfo.InvariantCulture),
                                            DateTime.Now.ToLongTimeString());


            if (File.Exists(path))
            {
                // Create a file to write to.
                using (StreamWriter sw = File.AppendText(path))
                {
                    // Received
                    sw.Write("................................" + newline);
                    sw.Write("--------- !Teleported! ---------" + newline);
                    sw.Write("................................" + newline);
                    sw.Write("Name of Character: " + StyxWoW.Me.Name + newline);
                    sw.Write("Level of Character: " + StyxWoW.Me.Level + newline);
                    sw.Write("Current Location: " + StyxWoW.Me.Location + newline);
                    sw.Write("Previous Location: " + Bots.Gatherbuddy.Profile.CurrentProfile.Current + newline);
                    sw.Write("Distance between Current and Previous Location: " +
                             StyxWoW.Me.Location.Distance(Bots.Gatherbuddy.Profile.CurrentProfile.Current) + newline);
                    sw.Write("Relative Location: " + StyxWoW.Me.RelativeLocation + " | World Location: " +
                             StyxWoW.Me.WorldLocation + newline);
                    sw.Write("Minimap Zone Text: " + StyxWoW.Me.MinimapZoneText + newline);
                    sw.Write("Current Map ID: " + StyxWoW.Me.MapId + " | Current Map Name: " + StyxWoW.Me.MapName +
                             newline);
                    sw.Write("Current Zone ID: " + StyxWoW.Me.ZoneId + " | Current Zone Name: " +
                             StyxWoW.Me.ZoneText + newline);
                    sw.Write("Is Dazed?: " + StyxWoW.Me.Dazed + newline +
                             " | Is Dead?: " + StyxWoW.Me.IsDead + newline +
                             " | Is Fleeing?: " + StyxWoW.Me.Fleeing + newline +
                             " | Is Falling?: " + StyxWoW.Me.IsFalling + newline +
                             " | Is Flying?: " + StyxWoW.Me.IsFlying + newline +
                             " | Is Inndoors?: " + StyxWoW.Me.IsIndoors + newline +
                             " | Is Outdoors?: " + StyxWoW.Me.IsOutdoors + newline +
                             " | Is Inside Instance?: " + StyxWoW.Me.IsInInstance + newline +
                             " | Is OnTransport?: " + StyxWoW.Me.IsOnTransport + newline +
                             " | Is Stunned?: " + StyxWoW.Me.Stunned + newline +
                             " | Is Mounted?: " + StyxWoW.Me.Mounted + newline);
                    sw.Write("------------------------------------------------------------------------");
                    sw.Write("------------------------------------------------------------------------");
                    sw.Write("| ***** Message Sent to Say: " + MIRsettings.Instance.Message + " | *****");
                    sw.Write("------------------------------------------------------------------------");
                    sw.Write("------------------------------------------------------------------------");


                    sw.Close();
                    Thread.Sleep(5000);
                    Lua.DoString(
                        String.Format("SendChatMessage(\"" + MIRsettings.Instance.Message +
                                      "\", \"SAY\", nil, \"\");"));
                    Thread.Sleep(3000);
                    KillProcess("HBRelog");
                    KillProcess("ARelog");
                    KillProcess("Wow");
                }
            }
            else
            {
                Directory.CreateDirectory(pathDirectory);
                File.AppendAllText(path, "File Created: " + timeSent + Environment.NewLine);
            }
        }

        public void KillProcess(string processName)
        {
            Logging.Write("Killing Process " + processName);
            Process[] processList = Process.GetProcessesByName(processName);
            foreach (Process process in processList)
            {
                process.Kill();
                Thread.Sleep(500);
            }
        }
    }
}